﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class OrganizationOath : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            this.lbWorkPoint.Text = ModuleUtil.GetWorkPointConent(this.CurrentStepID);
        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(this.CurrentStandID))
            {
                var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);

                var ps = DB.PartyJoinProcesss.Find(this.CurrentStepID);
                if (oModel != null)
                {
                    if (string.IsNullOrEmpty(oModel.SwornID))
                        this.dtSwornTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    else
                    {
                        var sworn = DB.PartyJoinSworns.Find(oModel.SwornID);
                        this.txtSwornAddress.Text = sworn.SwornAddress;
                        this.txaRemark.Text = sworn.Remark;
                        this.dtSwornTime.Text = sworn.SwornTime.ToString();
                    }
                }

                //页面只读显示
                if (this.ReadOnly.Trim() == "1")
                {
                    this.btnSubmit.Visible = false;
                    this.btnSaveClose.Visible = false;
                    //if (IsJoinPartyAdmin)
                    //{
                    //    this.btnSaveClose.Visible = true;
                    //}
                }
            }
        }


        private void SaveData(bool isClose, bool AdminSave = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过

            bool bResult = false;
            var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (oModel != null)
            {
                if (AdminSave)
                {
                    var sworn = DB.PartyJoinSworns.Find(oModel.SwornID);
                    if (sworn != null)
                    {
                        sworn.Remark = this.txaRemark.Text;
                        DB.SaveChanges();
                    }
                }
                else
                {
                    PartyJoinSworn sworn = new PartyJoinSworn();
                    sworn.ID = StringHelper.NewID;
                    sworn.SwornAddress = this.txtSwornAddress.Text.Trim();
                    sworn.Remark = this.txaRemark.Text.Trim();
                    sworn.SwornTime = Convert.ToDateTime(this.dtSwornTime.Text.Trim());
                    DB.PartyJoinSworns.Add(sworn);
                    bResult = DB.SaveChanges() > 0;

                    if (bResult)
                    {
                        DataSet ds = ModuleUtil.BranchLoginStantID(this.ApplyBranchID, this.CurrentStepID,this.CurrentStandID);
                        #region 组织宣誓和流程实例建立关系
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            var oModelEx = DB.PartyJoinLinkExamples.Find(dr["StandID"].ToString());

                            //保存时更新流程实例的会议ID
                            oModelEx.SwornID = sworn.ID; ;
                            DB.SaveChanges();
                        }
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            var oModelEx2 = DB.PartyJoinLinkExamples.Find(dr["StandID"].ToString());
                            oModel.Remark = "组织宣誓完成！";
                            oModel.HandleUserID = this.CurrentUser.LoginID;
                            oModel.HandleUserName = this.CurrentUser.UserName;
                            oModel.HandleTime = DateTime.Now;
                            oModel.Status = (int)Enum_FlowStanceStatus.通过;
                            bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                        }
                        #endregion
                    }
                }
            }
            if (bResult)
            { }
            else
            {
                if (!string.IsNullOrEmpty(Message))
                    Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
            }
        }


        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.SaveData(true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}